<template>
  <div class="navbar">
    <div class="left-nav">
      <!-- ！直接这样切换vuex存的变量，有点不太恰当，需要用到响应式变量存起来 -->
      <div @click="toggleSideBar">
        <i v-if="store.state.opened" class="el-icon-s-unfold size"></i>
        <i v-else class="el-icon-s-fold size"></i>
      </div>
      <Breadcrumb />
    </div>

    <!-- 右侧下拉菜单 -->
    <div class="right-menu">
      <el-dropdown class="avatar-container" trigger="click">
        <div class="avatar-wrapper">
          <span class="user-avatar">{{ username }} </span>
          <!-- <img src="/src/assets/logo.png" class="user-avatar" /> -->
          <!-- <i class="el-icon-caret-bottom" /> -->
          <el-icon>
            <ArrowDownBold />
          </el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu class="user-dropdown">
            <router-link to="/home">
              <el-dropdown-item> 首页 </el-dropdown-item>
            </router-link>
            <el-dropdown-item class="user-dropdown" divided @click="toLogout">
              <span style="display: block;">退出</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import Breadcrumb from "./Breadcrumb/index.vue";
// import { ref, computed, watch, watchEffect } from "vue";
import { useStore } from "vuex";
import { useRouter, useRoute } from "vue-router";
import { logout } from "@/api/login.js";
import { toast } from "utils/toast.js";
import { ref } from "vue";

const store = useStore();
let username = ref(null);
username = localStorage.getItem("username");

const toggleSideBar = () => {
  store.commit("toggleSideBar", !store.state.opened);
};

// let isCollapse = ref(store.state.opened);
//   watchEffect(() => {
//     isCollapse = store.state.opened;
//   })

const router = useRouter();
const route = useRoute();

const toLogout = () => {
  // 清空缓存，跳到登录页, 调用登出接口
  // router.push(`/login?redirect=${route.fullPath}`);
  logout().then((data) => {
    if (data.code === 200) {
      toast(data.msg);
      // Message({
      //   message: data.msg,
      //   type: 'success',
      //   duration: 2000
      // })
      localStorage.removeItem("token");
      router.push(`/login?redirect=${route.fullPath}`);
    } else {
      toast(data.msg, "error");
    }
  });
};
</script>

<style lang="scss" scoped>
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 50px;
  overflow: hidden;
  position: relative;
  background-color: #fff;
  // box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  margin-top: 6px;

  .left-nav {
    display: flex;
    align-items: center;

    .size {
      font-size: 28px;
    }
  }

  .right-menu {
    height: 100%;
    // line-height: 20px;
    display: flex;
    align-items: center;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;
        vertical-align: middle;
        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
          margin-right: 5px;
          color: #97a8be;
        }

        .el-icon {
          color: #97a8be;
          font-size: 12px;
          margin-top: 4px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
